package spring.boot.adminx.service;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import spring.boot.adminx.mapper.WxMiniAppConfigMapper;
import spring.boot.adminx.model.WxMiniAppConfig;

@Service
public class WxMiniAppConfigService extends ServiceImpl<WxMiniAppConfigMapper, WxMiniAppConfig> {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    public void updateValueByMiniIdAndTypeAndKey(Long wxMiniAppId, String type, String key, String value){
        UpdateWrapper<WxMiniAppConfig> wrapper = new UpdateWrapper<>();
        wrapper.set("value",value);
        wrapper.eq("wx_mini_app_id",wxMiniAppId);
        wrapper.eq("type",type);
        wrapper.eq("key_name",key);
        update(wrapper);
        delWxConfigKey(wxMiniAppId,type,key);
    }


    public void delWxConfigKey(Long wxMiniAppId, String type, String key){
        stringRedisTemplate.delete(getWxConfigKey(wxMiniAppId,type,key));
    }

    private String getWxConfigKey(Long wxMiniAppId, String type, String key){
        return "GUAGUA_PHOTO_CONFIG_"+wxMiniAppId+"_"+type+"_"+key;
    }

}
